import sys
from base.logtools import LogTools
# sys.path.append('../../spd2.8_web/')
from base.kwbase import Base
import openpyxl


class RunCase():
    def __init__(self, excel_object):
        self.base = Base()
        self.excel_object = excel_object
        # self.logtools = LogTools()

    def close_excel(self):
        self.excel_object.close()

    def runcase(self, sheetname):
        # 操作工作簿，打开路径
        # excel_object = openpyxl.load_workbook("../data/keywords_data.xlsx")
        # 获取sheet页
        sheet = self.excel_object[sheetname]
        for value in sheet.values:
            # 基于a列判断是否为测试用例
            if type(value[1]) is int:
                # 用一个字典接收参数
                kwargs = dict()
                kwargs['url'] = value[3]
                kwargs['method'] = value[4]
                kwargs['token'] = value[5]
                kwargs['jsondata'] = value[6]
                try:
                    res = self.base.do_request(kwargs)
                    print(sheetname, value[0], value[2], '请求成功')
                    assert self.base.key_get(res.json(), value[7]) == value[8], '断言失败'
                except Exception as e:
                    print(sheetname, value[0], value[2], '请求失败', e)
                    break
            # self.logtools.logger().error('执行成功；{a}，编号{b}'.format(a=sheetname, b=value[0]))
            elif value[1] is None:
                break


# self.logtools.logger().error('执行失败；{a}，编号{b}：{c}'.format(a=sheetname, b=value[0], c=value[2]))
# 获取指定单元格的值openpyxl
# value = sheet['A2'].value
# print(value)
# 最大行
# rows = sheet.max_row
# print(rows)
# 写入，基于单元格来进行写入
# sheet['H1'] = 'asd'
# 写入后记得保存：保存对象是excel
# excel.save("../data/keywords_data.xlsx")
# 关闭，释放


if __name__ == '__main__':
    rcase = RunCase(openpyxl.load_workbook("../../spd2.8_web/data/keywords_data.xlsx"))
    rcase.runcase('Sheet3')
    rcase.runcase('Sheet4')
    rcase.close_excel()
    rcase.close_broswer()
